package com.ehsunbehravesh.mypasswords.gui;

import com.ehsunbehravesh.mypasswords.Logger;
import com.ehsunbehravesh.mypasswords.Utils;
import com.ehsunbehravesh.mypasswords.exportimport.XMLImporter;
import java.io.File;
import java.util.Observable;
import java.util.Observer;
import java.util.Properties;
import javax.swing.JFrame;

public class ImportDialog extends javax.swing.JDialog implements Observer {

  private XMLImporter importer;

  public ImportDialog(java.awt.Frame parent, boolean modal, File file, String key, boolean allFieldsAreEncrypted) {
    super(parent, modal);
    importer = new XMLImporter(file, key, false, allFieldsAreEncrypted);
    initComponents();
    myInitComponents();
    Utils.setCenterOfParent((JFrame) getParent(), this);
  }

  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    pnlMain = new javax.swing.JPanel();
    btnCancel = new javax.swing.JButton();
    lblStatus = new javax.swing.JLabel();
    prgImport = new javax.swing.JProgressBar();
    lblTitle = new javax.swing.JLabel();
    chkOverWrite = new javax.swing.JCheckBox();
    btnStart = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    pnlMain.setBackground(new java.awt.Color(254, 254, 254));

    btnCancel.setFont(new java.awt.Font("Tahoma", 0, 13));
    btnCancel.setText("Cancel");
    btnCancel.setEnabled(false);
    btnCancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnCancelActionPerformed(evt);
      }
    });

    lblStatus.setFont(new java.awt.Font("Tahoma", 0, 10));

    lblTitle.setFont(new java.awt.Font("Tahoma", 1, 14));
    lblTitle.setText("Import from XML");

    chkOverWrite.setBackground(new java.awt.Color(255, 255, 255));
    chkOverWrite.setText("Overwrite existing passwords");

    btnStart.setFont(new java.awt.Font("Tahoma", 0, 13));
    btnStart.setText("Start Import");
    btnStart.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnStartActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain);
    pnlMain.setLayout(pnlMainLayout);
    pnlMainLayout.setHorizontalGroup(
      pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(pnlMainLayout.createSequentialGroup()
        .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addGroup(pnlMainLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlMainLayout.createSequentialGroup()
            .addGap(37, 37, 37)
            .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(prgImport, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlMainLayout.createSequentialGroup()
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addComponent(chkOverWrite)
                  .addComponent(lblTitle)
                  .addComponent(lblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)))))
        .addGap(39, 39, 39))
    );
    pnlMainLayout.setVerticalGroup(
      pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(pnlMainLayout.createSequentialGroup()
        .addGap(27, 27, 27)
        .addComponent(lblTitle)
        .addGap(18, 18, 18)
        .addComponent(chkOverWrite)
        .addGap(9, 9, 9)
        .addComponent(btnStart)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
        .addComponent(prgImport, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(18, 18, 18)
        .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(btnCancel)
          .addComponent(lblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(35, 35, 35))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
      setVisible(false);
      dispose();
}//GEN-LAST:event_btnCancelActionPerformed

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
      doImport();
    }//GEN-LAST:event_btnStartActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btnCancel;
  private javax.swing.JButton btnStart;
  private javax.swing.JCheckBox chkOverWrite;
  private javax.swing.JLabel lblStatus;
  private javax.swing.JLabel lblTitle;
  private javax.swing.JPanel pnlMain;
  private javax.swing.JProgressBar prgImport;
  // End of variables declaration//GEN-END:variables

  private void doImport() {
    importer.setOverWrite(chkOverWrite.isSelected());
    btnCancel.setEnabled(true);
    btnStart.setEnabled(false);
    chkOverWrite.setEnabled(false);

    Thread thread = new Thread(importer);
    thread.start();
    Logger.log("Import started: " + importer.getFile().getAbsolutePath());
  }

  private void myInitComponents() {
    MainFrame mainFrame = (MainFrame) getParent();
    if (mainFrame.getCurrentLanguage() != null) {
      //changeLanguage(mainFrame.getCurrentLanguage());
    }
    chkOverWrite.requestFocus();
    importer.addObserver(this);    
  }

  @Override
  public void update(Observable o, Object arg) {    
    if (arg.toString().equals("init")) {
      prgImport.setMinimum(importer.getMinProgress());
      prgImport.setMaximum(importer.getMaxProgress());
      prgImport.setValue(importer.getProgress());
    } else if (arg.toString().equals("progress")) {
      prgImport.setValue(importer.getProgress());
      lblStatus.setText(importer.getProgressPercent() + "%");
      if (importer.getProgress() == importer.getMaxProgress()) {
        btnCancel.setText("Close");
        lblStatus.setText(String.format("%s - Passwords: %d, Tags: %d", lblStatus.getText(), importer.getPasswordsCount(), importer.getTagsCount()));
        Logger.log("Import finished: " + importer.getFile().getAbsolutePath());
        Logger.log(String.format("PasswordsCount: %d - TagsCount: %d", importer.getPasswordsCount(), importer.getTagsCount()));
      }
    }
  }
  
  /*
  private void changeLanguage(Properties currentLanguage) {
    try {
      setDirection(currentLanguage.getProperty("direction"));
      setTitle(currentLanguage.getProperty("Browse"));
      
      
      refresh();
    } catch (Exception ex) {
      JOptionPane.showMessageDialog(this, "Error occured in changing language!", "Error", JOptionPane.ERROR_MESSAGE);
    }
  }
  
  private void setDirection(String direction) {
    if (direction.equals("rtl")) {
      setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
      scpMain.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
      
      for (int i = 0; i < scpMain.getComponents().length; i++) {
        Component component = scpMain.getComponents()[i];
        component.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
      }
    } else if (direction.equals("ltr")) {
      setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
      scpMain.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
      
      for (int i = 0; i < scpMain.getComponents().length; i++) {
        Component component = scpMain.getComponents()[i];
        component.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
      }
    }
  }

  private void refresh() {
    scpMain.setVisible(false);
    scpMain.setVisible(true);
  }*/
}
